#!/usr/bin/env python

import os
import re
import sys
import glob
import subprocess
import distutils.sysconfig


build_dir = "build/"

inc_dir = distutils.sysconfig.get_python_inc()
site_pkgs = distutils.sysconfig.get_python_lib()

procpypy = "procpy.py"

procpylibc = "_procpy.c"
procpylibso = "_procpy.so"

utmpmodulec = "utmpmodule.c"
utmpmoduleso = "utmp.so"

wutmpmodulec = "wutmpmodule.c"
wutmpmoduleso = "wutmp.so"

try:
   libprocso = glob.glob("/lib/libproc*.so") #["/lib/libproc.so.3.0.5"] #glob.glob("/lib/libproc*.so")
   if not libprocso:
      libprocso = glob.glob("/lib/libproc*")
      if not libprocso:
         print "Couldn't locate libproc!"
         sys.exit(1)
except:
   print "libproc not found!"
   sys.exit(1)

# more than one libproc found; pick the newest
if len(libprocso) > 1:
   libprocso.sort()
   
libprocso = libprocso.pop()

libprocver = re.search(r"\d.\d.\d", libprocso).group()
majmin = re.search(r"(\d).(\d).\d", libprocver)
major = majmin.group(1)
minor = majmin.group(2)

#v=r"#undef PROCPSVER31"
#v2=r"#undef PROCPSVER30"
v=''
if int(major) == 3 and int(minor)==1:
   v="\"#define PROCPSVER31\""
elif int(major) == 3 and int(minor)==0:
   v="\"#define PROCPSVER30\""
os.system("echo %s>procpsver.h" % (v))

if int(major) < 3:# and int(minor) < 2:
   print "Sorry, procpy won't work with your libproc. It's too old."
   sys.exit(1)

if not os.path.isdir("proc/%s" % (libprocver)):
   print "Couldn't locate libproc header files."
   sys.exit(1)

print "Copying the proc header files..."
os.system("cp proc/%s/*.h proc/" % (libprocver))

compile_generic = r"gcc -Wall -I%s -shared %s -o %s %s"
compile_procpylibc = compile_generic % (inc_dir, procpylibc,
                                        procpylibso, libprocso)
compile_utmpmodulec = compile_generic % (inc_dir, utmpmodulec,
                                         utmpmoduleso, "") # libprocso)
compile_wutmpmodulec = compile_generic % (inc_dir, wutmpmodulec,
                                         wutmpmoduleso, libprocso)

def run_gcc(compile_cmd):
   try:
      print compile_cmd
      retcode = subprocess.call(compile_cmd, shell=True)
      if retcode < 0:
         print >>sys.stderr, "Child was terminated by signal", -retcode
      else:
         print >>sys.stderr, "Compilation successful!"
   except OSError, e:
      print >>sys.stderr, "Compilation failed:", e


if not os.path.exists(build_dir):
   os.system("mkdir %s" % (build_dir))
else:
   print "Looks like procpylib is already built"
   sys.exit(0)


run_gcc(compile_procpylibc)
run_gcc(compile_utmpmodulec)
run_gcc(compile_wutmpmodulec)

if not os.getuid(): # UID==0
   print "Copying %s to %s" % (procpylibso, site_pkgs)
   os.system("cp %s %s" % (procpylibso, site_pkgs))
   print "Copying %s to %s" % (procpypy, site_pkgs)
   os.system("cp %s %s" % (procpypy, site_pkgs))
   print "Copying %s to %s" % (utmpmoduleso, site_pkgs)
   os.system("cp %s %s" % (utmpmoduleso, site_pkgs))
   print "Copying %s to %s" % (wutmpmoduleso, site_pkgs)
   os.system("cp %s %s" % (wutmpmoduleso, site_pkgs))
else:
   print "You don't have priviledges to copy the libraries to %s"%(site_pkgs)


print "Moving %s to %s" % (procpylibso, build_dir)
os.system("mv %s %s" % (procpylibso, build_dir))
print "Moving %s to %s" % (procpypy, build_dir)
os.system("cp %s %s" % (procpypy, build_dir))
print "Moving %s to %s" % (utmpmoduleso, build_dir)
os.system("mv %s %s" % (utmpmoduleso, build_dir))
print "Moving %s to %s" % (wutmpmoduleso, build_dir)
os.system("mv %s %s" % (wutmpmoduleso, build_dir))
